package com.aboo.action;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONObject;

import com.aboo.fusion.Column2D;
import com.aboo.fusion.Column3D;
import com.aboo.fusion.MSColumn2D;
import com.aboo.fusion.MSColumn3D;
import com.aboo.fusion.MSLine;
import com.aboo.fusion.Pie3D;
import com.aboo.fusion.ScrollLine2D;
import com.aboo.fusion.ScrollStackedColumn2D;
import com.aboo.fusion.StackedArea2D;
import com.aboo.fusion.StackedColumn3D;
import com.aboo.fusion.component.Application;
import com.aboo.fusion.component.Categories;
import com.aboo.fusion.component.Category;
import com.aboo.fusion.component.Chart;
import com.aboo.fusion.component.Data;
import com.aboo.fusion.component.Dataset;
import com.aboo.fusion.component.Definition;
import com.aboo.fusion.component.Line;
import com.aboo.fusion.component.Styles;
import com.aboo.fusion.component.TrendLines;
import com.aboo.fusion.constant.FusionConstants;
import com.aboo.pub.exception.BasicException;
import com.aboo.util.AjaxUtil;

public class ChartsExampleAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	
	public String msLine() throws BasicException {
		MSLine msLine = new MSLine();
		
		Chart chart = new Chart();
		chart.setCaption("MSLine");
		chart.setxAxisName("X轴");
		chart.setyAxisName("y轴");
		chart.setShowNames("1");
		chart.setNumberPrefix("$");
		chart.setBaseFontSize("14");
		msLine.setChart(chart);
		
		Categories categories = new Categories();
		Category category;
		for (int i = 1; i < 8; i ++) {
			category = new Category();
			category.setLabel(i + "");
			categories.addCategory(category);
		}
		msLine.addCategories(categories);
		
		Dataset dataset = new Dataset();
		dataset.setSeriesName("上周");
		dataset.setColor("1D0BD1");
		Data data;
		String value;
		for (int i = 1; i < 8; i ++) {
			data = new Data();
			value = "" + (int) (Math.random() * 200 + 100);
			data.setValue(value);
			data.setToolText("曲线：上周{br}数值：" + value);
			dataset.addData(data);
		}
		msLine.addDataSet(dataset);
		
		dataset = new Dataset();
		dataset.setSeriesName("本周");
		dataset.setColor("1D8BD1");
		for (int i = 1; i < 8; i ++) {
			data = new Data();
			value = "" + (int) (Math.random() * 200 + 100);
			data.setValue(value);
			data.setToolText("曲线：本周{br}数值：" + value);
			dataset.addData(data);
		}
		msLine.addDataSet(dataset);		
		
		Definition definition = new Definition();
		definition.setName("CaptionFont");
		definition.setSize("font");
		definition.setType("15");
		
		Application application = new Application();
		application.setToObject("CAPTION");
		application.setStyles("CaptionFont");
		
		Styles styles = new Styles();
		styles.addApplication(application);
		styles.addDefination(definition);		
		msLine.setStyles(styles);
		
		String jsonStr = new String("");
		if (chart != null) {
			JSONObject jo = JSONObject.fromObject(msLine);
			jsonStr = jo.toString();
		}
		
		AjaxUtil.doAjax(response, jsonStr);
		return null;
	}
	
	public String msColumn3D() throws BasicException {
		MSColumn3D msColumn3D = new MSColumn3D();
		
		Chart chart = new Chart();
		chart.setCaption("MSColumn3D");
		chart.setxAxisName("时间");
		chart.setyAxisName("产量");
		chart.setBaseFontSize("12");
		chart.setLabelDisplay(FusionConstants.CHART_LABELDIAPLAY_NONE);
		msColumn3D.setChart(chart);
		
		Categories categories = new Categories();
		categories.setFontSize("14");
		Category category;
		for (int i = 1; i < 13; i ++) {
			category = new Category();
			category.setLabel(i + "");
			categories.addCategory(category);
		}
		msColumn3D.addCategories(categories);
		
		Dataset dataset = new Dataset();
		dataset.setSeriesName("2011年");
		Data data;
		String value;
		for (int i = 1; i < 13; i ++) {
			data = new Data();
			value = "" + (int) (Math.random() * 4000 + 1000);
			data.setValue(value);
			data.setToolText("月份：" + i + "{br}值：" + value);
			dataset.addData(data);
		}
		msColumn3D.addDataSet(dataset);
		
		dataset = new Dataset();
		dataset.setSeriesName("2012年");
		for (int i = 1; i < 13; i ++) {
			data = new Data();
			value = "" + (int) (Math.random() * 4000 + 1000);
			data.setValue(value);
			data.setToolText("月份：" + i + "{br}值：" + value);
			dataset.addData(data);
		}
		msColumn3D.addDataSet(dataset);
		
		TrendLines trendLines = new TrendLines();
		Line line = new Line();
		line.setStartValue("1500");
		line.setDisplayValue("及格值");
		trendLines.addLine(line);		
		line = new Line();
		line.setStartValue("4500");
		line.setDisplayValue("优秀值");
		trendLines.addLine(line);
		msColumn3D.setTrendlines(trendLines);
		
		Styles styles = new Styles();
		Definition definition = new Definition();
		definition.setName("captionStyle");
		definition.setType("Bevel");
		definition.setSize("16");
		styles.addDefination(definition);
		Application application = new Application();
		application.setToObject(FusionConstants.STYLES_APPLICATION_TOOBJECT_CAPTION);
		application.setStyles("captionStyle");
		styles.addApplication(application);
		msColumn3D.setStyles(styles);
		
		String jsonStr = new String("");
		if (chart != null) {
			JSONObject jo = JSONObject.fromObject(msColumn3D);
			jsonStr = jo.toString();
		}
		
		AjaxUtil.doAjax(response, jsonStr);
		return null;
	}
	
	public String column2D() throws BasicException {
		Column2D fusionCharts = new Column2D();
		Chart chart = new Chart();
		chart.setCaption("Column2D");
		chart.setxAxisName("日期/天");
		chart.setyAxisName("电量 (KW.H)");
		chart.setLabelDisplay("none");
		chart.setFormatNumberScale("0");
		chart.setUseRoundedges("1");
		chart.setPalette("5");
		chart.setShowNames("1");
		chart.setBaseFontSize("14");
		fusionCharts.setChart(chart);	
	
		Data data;
		for(int i=0;i<=31;i++ ){
			data = new Data();
			String j = i+"";
			data.setLabel(j);
			data.setValue("" + (int) (Math.random() * 200 + 100));
			fusionCharts.addData(data);
		}	
		
		TrendLines tl = new TrendLines();
		Line line = new Line();
		line.setColor("00AACC");
		line.setDashed("1");
		line.setDisplayValue("警戒线");
		line.setStartValue("66");
		line.setEndValue("77");
		line.setValueOnRight("1");
		tl.addLine(line);
		fusionCharts.setTrendLines(tl);
		
		Definition definition = new Definition();
		definition.setName("CaptionFont");
		definition.setSize("font");
		definition.setType("15");
		
		Application application = new Application();
		application.setToObject("CAPTION");
		application.setStyles("CaptionFont");
		
		Styles styles = new Styles();
		styles.setApplication(new ArrayList<Application>());
		styles.getApplication().add(application);
		styles.setDefinition(new ArrayList<Definition>());
		styles.getDefinition().add(definition);
		fusionCharts.setStyles(styles);
		
		String jsonStr = new String("");
		if (chart != null) {
			JSONObject jo = JSONObject.fromObject(fusionCharts);
			jsonStr = jo.toString();
		}
		
		AjaxUtil.doAjax(response, jsonStr);
		return null;
	}
	
	public String pie3D() throws BasicException {
		
		Pie3D fusioncharts = new Pie3D();
		
		Chart chart = new Chart();
		chart.setCaption("Pie3D");
		chart.setPalette("4");
		chart.setFormatNumberScale("0");
		chart.setShowPercentInToolTip("1");
		chart.setPieSliceDepth("30");
		chart.setPieYScale("60");
		fusioncharts.setChart(chart);
		
		Data data;
		data = new Data();
		data.setLabel("first");
		data.setValue("151");
		data.setIssliced("1");
		fusioncharts.addData(data);
		data = new Data();
		data.setLabel("second");
		data.setValue("152");
		fusioncharts.addData(data);
		data = new Data();
		data.setLabel("third");
		data.setValue("173");
		data.setIssliced("0");
		fusioncharts.addData(data);
		
		String jsonStr = new String("");
		if (chart != null) {
			JSONObject jo = JSONObject.fromObject(fusioncharts);
			jsonStr = jo.toString();
		}
		
		AjaxUtil.doAjax(response, jsonStr);
		return null;
	}
	
	public String column3D() throws BasicException
	{
		Chart chart = new Chart();
		chart.setCaption("Column3D");
		chart.setyAxisName("单位：万千瓦时");
		chart.setxAxisName("日期");
		chart.setPalette("1");
		chart.setFormatNumberScale("0");
		chart.setNumDivLines("3");
		chart.setDecimalPrecision("0");

//		chart.setShowLegend("1");
		chart.setLegendPosition("BOTTOM");

//		chart.setDecimals("2");
//		chart.setShowBorder("1");
//		chart.setImageSava("1");
//		chart.setImageSavaURL("http://localhost:8080/DSM_WuXi/wuxi/test.jsp");
		
		List<Data> list_data = new ArrayList<Data>();
		Data data;
		
		data = new Data();
		data.setLabel("14");
		data.setValue("-1212.083");
		list_data.add(data);
		
		data = new Data();
		data.setLabel("15");
		data.setValue("22345.1222");
		list_data.add(data);
		
		data = new Data();
		data.setLabel("16");
		data.setValue("32345.1");
		list_data.add(data);
		
		data = new Data();
		data.setLabel("17");
		data.setValue("42345");
		list_data.add(data);
		
		data = new Data();
		data.setLabel("18");
		data.setValue("22345");
		list_data.add(data);
		
		TrendLines trendLines = new TrendLines();
		Line line = new Line();
		line.setStartValue("30000");
		line.setDisplayValue("预警值");
		line.setColor("339900");
		line.setValueOnRight("1");
		
		trendLines.addLine(line);
		
		Column3D column3d = new Column3D();
		column3d.setChart(chart);
		column3d.setData(list_data);
		column3d.setTrendLines(trendLines);
		
		String jsonStr = new String();
		if(null != chart)
		{
			JSONObject jo = JSONObject.fromObject(column3d);
			
			jsonStr = jo.toString();
		}
		AjaxUtil.doAjax(response, jsonStr);
		
		return null;
	}
	
	public String scrollLine2D() throws BasicException {
		/** 设置报表参数 */
		Chart chart = new Chart();
		chart.setBaseFont("宋体");
		chart.setBaseFontSize("13");
		chart.setCaption("ScrollLine2D");
		chart.setxAxisName("时间");
		chart.setyAxisName("电量");
		chart.setShowNames("1");
		chart.setLabelStep("1");
		chart.setyAxisValuesStep("0");
		chart.setyAxisMaxValue("80");
		chart.setNumberPrefix("kw");
		chart.setShowAlternateHGridColor("0");
		/** 趋势线1参数 */
		Line line = new Line();
		TrendLines tl = new TrendLines();
		line.setDashed("1");
		line.setColor("1D7BD1");
		line.setDisplayValue("趋势线");
		line.setStartValue("25");
		tl.addLine(line);
		/** 趋势线2参数 */
		Line line1 = new Line();
		line1.setDashed("1");
		line1.setColor("1D7BD1");
		line1.setDisplayValue("趋势线");
		line1.setStartValue("18");
		tl.addLine(line1);

		/**X轴lable数值*/
		List<Categories> cateList = new ArrayList<Categories>();
		Categories cates = new Categories();
		for (int i = 0; i < 24; i++) {
			Category cate = new Category();
			cate.setLabel(i + ":00");
			cates.addCategory(cate);
			cateList.add(cates);
		}

		/**报表数据1*/
		Data data = new Data();
		Dataset today = new Dataset();
		today.setSeriesName("今天");
		today.setColor("8D8FD1");
		for (int i = 0; i < 24; i++) {
			data = new Data();
			data.setValue(i + "");
			today.addData(data);
			i++;
		}
		/**报表数据2*/
		Dataset dataset2 = new Dataset();
		dataset2.setSeriesName("昨天");
		dataset2.setColor("3D8BD2");
		for (int i = 40; i >= 0; i--) {
			data = new Data();
			data.setValue(i + "");
			dataset2.addData(data);
			i--;
		}

		/*自定义样式，使用默认的无需写
		 * Definition definition = new Definition();
		 * definition.setName("CaptionFont"); definition.setSize("font");
		 * definition.setType("15");
		 * 
		 * Application application = new Application();
		 * application.setToobject("CAPTION");
		 * application.setStyles("CaptionFont");
		 * 
		 * Styles styles = new Styles(); styles.setApplication(new
		 * ArrayList<Application>()); styles.getApplication().add(application);
		 * styles.setDefinition(new ArrayList<Definition>());
		 * styles.getDefinition().add(definition);
		 */

		/**MSColumn2D.swf*/
		// MSColumn2D ms=new MSColumn2D();
		// ms.setChart(chart);
		// ms.setCategories(cates);
		// ms.addDataSet(today);
		// ms.addDataSet(dataset2);
		// ms.setTrendLines(tl);
		
		/**ScrollLine2D .swf*/
		ScrollLine2D scoll = new ScrollLine2D();
		scoll.setChart(chart);
		scoll.setCategories(cateList);
		scoll.addDataSet(today);
		scoll.addDataSet(dataset2);
		scoll.setTrendLines(tl);

		String jsonScoll = new String("");
		if (chart != null) {
			JSONObject jo = JSONObject.fromObject(scoll);
			jsonScoll = jo.toString();
		}
	
		AjaxUtil.doAjax(response, jsonScoll);
		return null;
	}
	
	public String mSColumn2D() throws BasicException {
		/** 设置报表参数 */
		Chart chart = new Chart();
		chart.setBaseFont("宋体");
		chart.setBaseFontSize("13");
		chart.setCaption("MSColumn2D");
		chart.setxAxisName("时间");
		chart.setyAxisName("电量");
		chart.setShowNames("1");
		chart.setLabelStep("1");
		chart.setyAxisValuesStep("0");
		chart.setyAxisMaxValue("80");
		 chart.setLabelDisplay("none");
		chart.setNumberPrefix("kw");
		chart.setShowAlternateHGridColor("0");
		/** 趋势线1参数 */
		Line line = new Line();
		TrendLines tl = new TrendLines();
		line.setDashed("1");
		line.setColor("1D7BD1");
		line.setDisplayValue("趋势线");
		line.setStartValue("25");
		tl.addLine(line);
		/** 趋势线2参数 */
		Line line1 = new Line();
		line1.setDashed("1");
		line1.setColor("1D7BD1");
		line1.setDisplayValue("趋势线");
		line1.setStartValue("18");
		tl.addLine(line1);

		/**X轴lable数值*/
		List<Categories> cateList = new ArrayList<Categories>();
		Categories cates = new Categories();
		for (int i = 0; i < 24; i++) {
			Category cate = new Category();
			cate.setLabel(i + ":00");
			cates.addCategory(cate);
			cateList.add(cates);
		}

		/**报表数据1*/
		Data data = new Data();
		Dataset today = new Dataset();
		today.setSeriesName("今天");
		today.setColor("8D8FD1");
		for (int i = 0; i < 24; i++) {
			data = new Data();
			data.setValue(i + "");
			today.addData(data);
			i++;
		}
		/**报表数据2*/
		Dataset dataset2 = new Dataset();
		dataset2.setSeriesName("昨天");
		dataset2.setColor("3D8BD2");
		for (int i = 40; i >= 0; i--) {
			data = new Data();
			data.setValue(i + "");
			dataset2.addData(data);
			i--;
		}

		/*自定义样式，使用默认的无需写
		 * Definition definition = new Definition();
		 * definition.setName("CaptionFont"); definition.setSize("font");
		 * definition.setType("15");
		 * 
		 * Application application = new Application();
		 * application.setToobject("CAPTION");
		 * application.setStyles("CaptionFont");
		 * 
		 * Styles styles = new Styles(); styles.setApplication(new
		 * ArrayList<Application>()); styles.getApplication().add(application);
		 * styles.setDefinition(new ArrayList<Definition>());
		 * styles.getDefinition().add(definition);
		 */

		/**MSColumn2D.swf*/
		 MSColumn2D ms=new MSColumn2D();
		 ms.setChart(chart);
		 ms.addCategories(cates);
		 ms.addDataSet(today);
		 ms.addDataSet(dataset2);
		 ms.setTrendLines(tl);
		

		String jsonScoll = new String("");
		if (chart != null) {
			JSONObject jo = JSONObject.fromObject(ms);
			jsonScoll = jo.toString();
		}
	
		AjaxUtil.doAjax(response, jsonScoll);
		return null;
	}
	
	public String scrollStackedColumn2D() throws BasicException {
		ScrollStackedColumn2D fusionCharts = new ScrollStackedColumn2D();
		
		Chart chart = new Chart();
		chart.setPalette("3");
		chart.setCaption("ScrollStackedColumn2D");
		chart.setxAxisName("Day");
		chart.setNumberPrefix("$");
		chart.setUseRoundedges("1");
		chart.setShowValues("0");
		fusionCharts.setChart(chart);
		
		Categories categories = new Categories();
		categories.setFontSize("14");
		Category category;
		for (int i = 1; i < 13; i ++) {
			category = new Category();
			category.setLabel(i + "");
			categories.addCategory(category);
		}
		fusionCharts.addCategories(categories);
		
		Dataset dataset = new Dataset();
		dataset.setSeriesName("2011年");
		Data data;
		String value;
		for (int i = 1; i < 13; i ++) {
			data = new Data();
			value = "" + (int) (Math.random() * 4000 + 1000);
			data.setValue(value);
			data.setToolText("月份：" + i + "{br}值：" + value);
			dataset.addData(data);
		}
		fusionCharts.addDataSet(dataset);
		
		Dataset dataset1 = new Dataset();
		dataset1.setSeriesName("2012年");
		for (int i = 1; i < 13; i ++) {
			data = new Data();
			value = "" + (int) (Math.random() * 4000 + 1000);
			data.setValue(value);
			data.setToolText("月份：" + i + "{br}值：" + value);
			dataset1.addData(data);
		}
		fusionCharts.addDataSet(dataset1);
		
		Dataset dataset2 = new Dataset();
		dataset2.setSeriesName("2013年");
		for (int i = 1; i < 13; i ++) {
			data = new Data();
			value = "" + (int) (Math.random() * 4000 + 1000);
			data.setValue(value);
			data.setToolText("月份：" + i + "{br}值：" + value);
			dataset2.addData(data);
		}
		fusionCharts.addDataSet(dataset2);
		
		
		String jsonScoll = new String("");
		if (chart != null) {
			JSONObject jo = JSONObject.fromObject(fusionCharts);
			jsonScoll = jo.toString();
		}
	
		AjaxUtil.doAjax(response, jsonScoll);
		return null;
	}
	
	public String stackedArea2D() throws BasicException {
		/** 设置报表参数 */
		Chart chart = new Chart();
		chart.setBaseFont("宋体");
		chart.setBaseFontSize("13");
		chart.setCaption("StackedArea2D");
		chart.setxAxisName("时间");
		chart.setyAxisName("电量");
		chart.setShowNames("1");
		chart.setLabelStep("1");
		chart.setyAxisValuesStep("0");
		chart.setyAxisMaxValue("80");
		 chart.setLabelDisplay("none");
		chart.setNumberPrefix("kw");
		chart.setShowAlternateHGridColor("0");
		/** 趋势线1参数 */
		Line line = new Line();
		TrendLines tl = new TrendLines();
		line.setDashed("1");
		line.setColor("1D7BD1");
		line.setDisplayValue("趋势线");
		line.setStartValue("25");
		tl.addLine(line);
		/** 趋势线2参数 */
		Line line1 = new Line();
		line1.setDashed("1");
		line1.setColor("1D7BD1");
		line1.setDisplayValue("趋势线");
		line1.setStartValue("18");
		tl.addLine(line1);

		/**X轴lable数值*/
		List<Categories> cateList = new ArrayList<Categories>();
		Categories cates = new Categories();
		for (int i = 0; i < 24; i++) {
			Category cate = new Category();
			cate.setLabel(i + ":00");
			cates.addCategory(cate);
			cateList.add(cates);
		}

		/**报表数据1*/
		Data data = new Data();
		Dataset today = new Dataset();
		today.setSeriesName("今天");
		today.setColor("8D8FD1");
		for (int i = 0; i < 24; i++) {
			data = new Data();
			data.setValue(i + "");
			today.addData(data);
			i++;
		}
		/**报表数据2*/
		Dataset dataset2 = new Dataset();
		dataset2.setSeriesName("昨天");
		dataset2.setColor("3D8BD2");
		for (int i = 40; i >= 0; i--) {
			data = new Data();
			data.setValue(i + "");
			dataset2.addData(data);
			i--;
		}

		/*自定义样式，使用默认的无需写
		 * Definition definition = new Definition();
		 * definition.setName("CaptionFont"); definition.setSize("font");
		 * definition.setType("15");
		 * 
		 * Application application = new Application();
		 * application.setToobject("CAPTION");
		 * application.setStyles("CaptionFont");
		 * 
		 * Styles styles = new Styles(); styles.setApplication(new
		 * ArrayList<Application>()); styles.getApplication().add(application);
		 * styles.setDefinition(new ArrayList<Definition>());
		 * styles.getDefinition().add(definition);
		 */

		/**MSColumn2D.swf*/
		StackedArea2D ms=new StackedArea2D();
		 ms.setChart(chart);
		 ms.addCategories(cates);
		 ms.addDataSet(today);
		 ms.addDataSet(dataset2);
		 ms.setTrendLines(tl);
		

		String jsonScoll = new String("");
		if (chart != null) {
			JSONObject jo = JSONObject.fromObject(ms);
			jsonScoll = jo.toString();
		}
	
		AjaxUtil.doAjax(response, jsonScoll);
		return null;
	}
	
	public String stackedColumn3D() throws BasicException{
		StackedColumn3D stackedColumn3D = new StackedColumn3D();
		
		Chart chart = new Chart();
		chart.setCaption("StackedColumn3D");
		chart.setShowValues("0");
		chart.setFormatNumberScale("0");
		stackedColumn3D.setChart(chart);
		
		Categories categories = new Categories();
		
		Category category = new Category();
		category.setLabel("Jan");
		categories.addCategory(category);
		
		category = new Category();
		category.setLabel("Feb");
		categories.addCategory(category);
		
		category = new Category();
		category.setLabel("Mar");
		categories.addCategory(category);
		stackedColumn3D.addCategories(categories);
		
		Dataset dataset = new Dataset();
		dataset.setSeriesName("Google");
		dataset.setColor("00FF33");
		
		Data data = new Data();
		data.setValue("3040");
		dataset.addData(data);
		
		data = new Data();
		data.setValue("2794");
		dataset.addData(data);
		
		data = new Data();
		data.setValue("3026");
		dataset.addData(data);
		stackedColumn3D.addDataSet(dataset);
		
		dataset = new Dataset();
		dataset.setSeriesName("Yahoo");
		dataset.setColor("FF0033");
		
		data = new Data();
		data.setValue("1200");
		dataset.addData(data);
		
		data = new Data();
		data.setValue("1124");
		dataset.addData(data);
		
		data = new Data();
		data.setValue("1006");
		dataset.addData(data);
		stackedColumn3D.addDataSet(dataset);
		
		dataset = new Dataset();
		dataset.setSeriesName("MSN");
		dataset.setColor("0033FF");
		
		data = new Data();
		data.setValue("600");
		dataset.addData(data);
		
		data = new Data();
		data.setValue("724");
		dataset.addData(data);
		
		data = new Data();
		data.setValue("806");
		dataset.addData(data);
		stackedColumn3D.addDataSet(dataset);
		
		dataset = new Dataset();
		dataset.setSeriesName("Others");
		dataset.setColor("FF3300");
		
		data = new Data();
		data.setValue("965");
		dataset.addData(data);
		
		data = new Data();
		data.setValue("771");
		dataset.addData(data);
		
		data = new Data();
		data.setValue("732");
		dataset.addData(data);
		stackedColumn3D.addDataSet(dataset);
		
		String jsonStr = new String("");
		if (chart != null) {
			JSONObject jo = JSONObject.fromObject(stackedColumn3D);
			jsonStr = jo.toString();
		}
		
		AjaxUtil.doAjax(response, jsonStr);
		return null;
	}
}
